import _ from 'lodash'
import { login,refresh,logout,gettingPhone,loginByApp } from 'api/authApi'
import * as authHelper from 'helpers/authHelper'
import { authChecker } from "helpers/authHelper"


const getDefaultState = () => {
	return {
		wxCode: null,
		inviteString: null,
		inviteCode: null,
		inviteProductId: null,
		accessToken: authHelper.getToken(), //获取本地存储的 access_token
		accessTokenRefreshAt: authHelper.getTokenRefreshAt(), //获取本地存储的刷新 access_token_refresh_at
		accessTokenExpiredAt: authHelper.getTokenExpiredAt(), //获取本地存储的到期 access_token_expired_at
		phoneNum: null,
		openid: null,
		unionid: null,
		statusBar:0
	}
}

export default {
	state: getDefaultState(), //初始化状态管理数据
	getters: { //处理后的状态
		wxCode: state => state.wxCode,
		inviteString: state => state.inviteString,
		inviteCode: state => state.inviteCode,
		inviteProductId: state => state.inviteProductId,
		isLogin: async () => {
			return await authChecker()
		},
		accessToken: state => state.accessToken,
		accessTokenRefreshAt: state => state.accessTokenRefreshAt,
		accessTokenExpiredAt: state => state.accessTokenExpiredAt,
		phoneNum: state => state.phoneNum,
		openid: state => state.openid,
		unionid: state => state.unionid,
		statusBar: state => state.statusBar
	},
	mutations: {
		setWxCode(state, wxCode) {
			state.wxCode = wxCode
		},
		setPhoneNum(state, phoneNum) {
			state.phoneNum = phoneNum
		},
		setOpenId(state, openid) {
			state.openid = openid
		},
		setUnionId(state, unionid) {
			state.unionid = unionid
		},
		setInviteString(state, inviteString) {
			state.inviteString = inviteString
		},
		setInviteCode(state, inviteCode) {
			state.inviteCode = inviteCode
		},
		setInviteProductId(state, inviteProductId) {
			state.inviteProductId = inviteProductId
		},
		setToken(state, tokenPayload) {
			state.accessToken = tokenPayload.access_token
			state.accessTokenRefreshAt = new Date().getTime() + tokenPayload.refresh_at * 1000
			state.accessTokenExpiredAt = new Date().getTime() + tokenPayload.expired_at * 1000
		},
		resetState: (state) => {
			Object.assign(state, getDefaultState())
		},
		setStatusBar(state, statusbar){
			state.statusBar = statusbar
		}
	},
	actions: {
		async getPhone({ dispatch,commit }, params = {}) {
			if (_.isEmpty(params.code)) { //检测是否获取到了uni.login()的code
				const uniLogin = await uni.login()
				params.code = uniLogin[1].code //取值存在？
			}
			const phoneRes = await gettingPhone(params)
			
			if(phoneRes.hasOwnProperty('phone')){
				commit('setPhoneNum', phoneRes.phone)
				commit('setOpenId', phoneRes.openid)
				commit('setUnionId', phoneRes.unionid)
			}else{
				commit('setToken', phoneRes)
				authHelper.setToken(phoneRes)
			}
		},
		async login({dispatch,commit }, params = {}) {
			
			const loginResponse = await login(params) //登录完成
			if(!loginResponse.message){
				commit('setToken', loginResponse)
				authHelper.setToken(loginResponse)
			}else{
				uni.showModal({
					title:'提示',
					content:loginResponse.message,
					confirmText:'我知道了',
					confirmColor:'#FF6011',
					showCancel:false
				})
			}
		},
		async refresh({ commit,state }) {
			const refreshResponse = await refresh(state.accessToken)
			commit('setToken', refreshResponse)
			authHelper.setToken(refreshResponse)
		},
		async logout({ commit,state }) {
			await logout(state.accessToken)
			await authHelper.logout()
			commit('resetState')
		}
	}
}
